package org.colcon.web.model.parsers;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.colcon.web.model.framework.IItem;
import org.colcon.web.server.CLI;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
public class XMLParser 
{
	public static String xmlToCli(String filepath)
	{
		String str = "";
		
		try 
		{
			File fXmlFile = new File(filepath);
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
			 
			NodeList children = doc.getChildNodes();
			
			for(int i = 0; i < children.getLength(); i++)
				str += getNodeString(children.item(i));
		}
		catch (Exception e) 
		{
			e.printStackTrace();	  
		}
		
		return str;
	}
	
	public static String getNodeString(Node node)
	{
		String key = "-" + node.getNodeName();
		String value = "";
	
		NodeList children = node.getChildNodes();
		
		if(children.getLength() == 1)
			value = children.item(0).getNodeValue();
		else
		{
			for(int i = 1; i < children.getLength(); i++) 
			{
				Node child = children.item(i);
				
				if (child.getNodeType() == Node.ELEMENT_NODE) 
					value += getNodeString(child) + " ";
			}
		}
		
		value = value.trim();
		
		if(value.split(" ").length <= 1)
			return key + " " + value;
		else
			return key + " {" + value + "}";
	}
	
	public static IItem[] loadItems(String filepath)
	{
		String cli = xmlToCli(filepath);
		
		Map<String, String> map = CLIParser.parse(cli);
		
		if(!map.containsKey("items"))
			return null;
		
		map = CLIParser.parse(map.get("items"));
		
		if(!map.containsKey("item"))
			return null;
		
		String[] str = map.get("item").split(";");
		
		IItem[] items = new IItem[str.length];
		
		for(int i = 0; i < items.length; i++)
			items[i] = CLIParser.parseItem(str[i]);
		
		return items;
	}
	
	public static String[] loadInitCommands(String type)
	{
		return loadCommands("xml/init/" + type + ".xml");
	}
	
	public static String[] loadCommands(String filepath)
	{
		String cli = xmlToCli(filepath);
		
		Map<String, String> map = CLIParser.parse(cli);
		
		if(!map.containsKey("commands"))
			return null;
		
		map = CLIParser.parse(map.get("commands"));
		
		if(!map.containsKey("command"))
			return null;
		
		List<String> commands = new LinkedList<String>();
		
		for(String str : map.get("command").split(";"))
		{
			str = str.trim();
			
			String cmd = CLIParser.parseCmd(str);
			
			if(cmd == null)
				continue;
			
			if(cmd.equals(CLI.LOAD))
			{
				String path = CLIParser.parsePath(str);
				
				if(path == null)
					continue;
				
				Map<String, String> commandMap = CLIParser.parse(str);
				
				String file = commandMap.get("file");
					
				if(file == null)
					continue;
				
				for(String addCommand : getAddCommands(loadItems(file), path))
					commands.add(addCommand);
			}
			else
			{
				commands.add(str);
			}
		}
		
		return commands.toArray(new String[0]);
	}
	
	public static String[] getAddCommands(IItem[] items, String path)
	{
		if(items == null)
			return null;
		
		String[] commands = new String[items.length];
		
		for(int i = 0; i < commands.length; i++)
			commands[i] = "-cmd add -path " + path + " -item {" + items[i].toString() + "}";
		
		return commands;
	}
	
	//TODO: implement CLI -> XML
	/*
	public static String cliToXml(String cli)
	{
		String xml = "";
		Map<String, String> args = CLI.parse(cli);
		
		for(String key : args.keySet())
			xml += "<" + key + ">" + args.get(key) + "</" + key + ">";
		
		return xml;
	}
	*/
}

